from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from db import db1


# Create your views here.


def show_customer(request):
    sql = "select a.name,a.money,a.company_name,a.status from customer as a left join " \
          "reg_form as b on a.refromid=b.id left join customer_manager as ab on ab.id=a.manger_id"
    res = db1.find_all(sql)
    return JsonResponse({'code': 200, 'list': res})


import json


def audit(request):
    import time
    # 获取参数
    data = json.loads(request.body)
    time = int(time.time())
    ssql = "select * from customer where id=%d" % (int(data['id']))
    customer = db1.find(ssql)

    # 更新constmer表，状态，审核人，审核时间
    sql = "update customer set status=%d,audit_time=%d,audit_name='zs' where id=%d" % (
        int(data['is_agree']), time, int(data['id']))
    db1.update(sql)

    # 更新来源表
    sql1 = "select id,tcount from reg_form where id=%d" % (int(customer['refromid']))
    regform = db1.find(sql1)
    number = int(regform['tcount']) + 1
    sql2 = 'update reg_form set tcount=%d where id=%d' % (int(number), int(customer['refromid']))
    db1.update(sql2)
    # 更新客户经理中的总金额
    sql3 = "select * from customer_manager where id=%d" % (int(customer['manager_id']))
    manager = db1.find(sql3)
    number = int(manager['tmoney']) + int(customer['tmoney'])

    sql4 = 'update customer_manager set tmoney=%d where id=%d' % (int(number), int(customer['manager_id']))
    db1.update(sql4)
    return JsonResponse({"code": 200})
